package com.baizhi.controller;

import com.baizhi.entity.Address;
import com.baizhi.entity.User;
import com.baizhi.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("front/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @RequestMapping("showAddress")
    public String showAddress(HttpSession session, Integer userId){
        List<Address> addresses = addressService.selectAddressByUserId(userId);
        session.setAttribute("address",addresses);
        return "redirect:/front/order/place-order.jsp";
    }
    @RequestMapping("addAddress")
    public String addAddress(HttpSession session,Address address){
        User user =(User) session.getAttribute("user");
        address.setUserId(user.getUserId());
        addressService.addAddress(address);
        return "redirect:/front/address/showAddress.do?userId="+user.getUserId()+"";
    }
    @RequestMapping("updateAddress")
    public String updateAddress(HttpSession session,Address address){
        User user =(User) session.getAttribute("user");
        address.setUserId(user.getUserId());
        addressService.updateAddress(address);
        return "redirect:/front/address/showAddress.do?userId="+user.getUserId()+"";
    }
    @RequestMapping("removeAddress")
    public String removeAddress(HttpSession session,Integer id){
        User user =(User) session.getAttribute("user");
        addressService.removeAddress(id);
        return "redirect:/front/address/showAddress.do?userId="+user.getUserId()+"";
    }
}
